package 面向对象.TestEight;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * @author IT_Jay
 * @date 2021/11/26 20:40
 */

public class Frame5 {
    static JMenuItem item1,item2,item3,item4,item5,item6,item7,item8;
    static JTextArea textArea;
    static JFrame frame;

    public static void main(String[] args) {
        Frame5 frame5 = new Frame5();
        frame5.init();
    }

    public void init(){
        frame = new JFrame("按键响应菜单栏");
        frame.setSize(500,300);

        JMenuBar menuBar = new JMenuBar();
        JMenu menu =new JMenu("菜单1");
        item1 = new JMenuItem("按钮1(F1)");
        item2 = new JMenuItem("按钮1(F2)");
        item3 = new JMenuItem("按钮1(F3)");
        menu.add(item1);
        menu.add(item2);
        menu.add(item3);

        JMenu menu1 = new JMenu("菜单2");
        item4 = new JMenuItem("按钮4(F4)");
        item5 = new JMenuItem("按钮4(F5)");
        item6 = new JMenuItem("按钮4(F6)");
        menu1.add(item4);
        menu1.add(item5);
        menu1.add(item6);

        JMenu menu2 = new JMenu("菜单3");
        item7 = new JMenuItem("按钮7(F7)");
        menu2.add(item7);

        JMenu menu3 = new JMenu("菜单4");
        item8 = new JMenuItem("按钮8(F8)");
        menu3.add(item8);

        menuBar.add(menu);
        menuBar.add(menu1);
        menuBar.add(menu2);
        menuBar.add(menu3);
        textArea = new JTextArea(10,10);
        textArea.setEditable(false);
        frame.add(textArea);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setJMenuBar(menuBar);
        addButtonListener();
        addKeyListener();
        frame.setVisible(true);
    }

    public void addButtonListener(){
        item1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("触发了按钮1");
            }
        });

        item2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("触发了按钮2");
            }
        });

        item3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("触发了按钮3");
            }
        });

        item4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("触发了按钮4");
            }
        });

        item5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("触发了按钮5");
            }
        });

        item6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("触发了按钮6");
            }
        });

        item7.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("触发了按钮7");
            }
        });

        item8.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("触发了按钮8");
            }
        });
    }
    public void addKeyListener(){
        textArea.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {

            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == 112) textArea.setText("触发了按钮1");
                else if (e.getKeyCode() == 113) textArea.setText("触发了按钮2");
                else if (e.getKeyCode() == 114) textArea.setText("触发了按钮3");
                else if (e.getKeyCode() == 115) textArea.setText("触发了按钮4");
                else if (e.getKeyCode() == 116) textArea.setText("触发了按钮5");
                else if (e.getKeyCode() == 117) textArea.setText("触发了按钮6");
                else if (e.getKeyCode() == 118) textArea.setText("触发了按钮7");
                else if (e.getKeyCode() == 119) textArea.setText("触发了按钮8");

            }
        });
    }
}
